
<template>
  <div>
    <div class="qin">
      <div id="toubu">
        <span @click="go0ff()"> < 返回</span>修改增加收货地址
      </div>
    </div>
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
    />
    
  </div>
</template>
 
<script>
import { Toast } from "vant";
import axios from "axios";
import { getAddress } from "../../api/address";

export default {
  data() {
    return {
      chosenAddressId: "1",
      page: 1,
      list: [],
    };
  },
  created() {
    this.init();
  },
  methods: {
    go0ff() {
      if (window.history.length <= 1) {
        this.$router.push({ path: "/" });
        return false;
      } else {
        this.$router.go(-1);
      }
    },
    async onAdd() {
      this.$router.push("/location");
    },

    async onEdit(item, index) {
      let id = this.list[index].id;
        this.$router.push({
        path: "/location",
        query: { id },
      });
      
      
    },
    async init() {
      const res = await getAddress({ per: 10, page: this.page });

      for (var i = 0; i < res.addresses.length; i++) {
        var obj = {
          name: "",//姓名
          tel: "",//手机号
          address: "",//收货地址
          isDefault: "",//是否为默认住址
        };
        obj.name = res.addresses[i].receiver;
        obj.tel = res.addresses[i].mobile;
        obj.address =  res.addresses[i].regions+res.addresses[i].address;
        obj.isDefault = res.addresses[i].isDefault;
        obj.id = res.addresses[i]._id;
        this.list.push(obj);
         
      }
     
      console.log(res.addresses);
    },
  },

  mounted() {
    var data1 = JSON.parse(localStorage.getItem("address"));
  },
};
</script>
<style scoped>
.qin {
  width: 100%;
  height: 50px;
  background: white;
}
#toubu {
  width: 100%;
  height: 50px;
  text-align: center;
  font-size: 16px;
  line-height: 50px;
  position: relative;
}
#toubu > span {
  position: absolute;
  font-size: 14px;
  line-height: 50px;
  color: blue;
  left: 10px;
}
</style>
 